package com.shz.controller;


import cn.hutool.core.util.StrUtil;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shz.dto.MyAnswer;
import com.shz.dto.MyAnswerList;
import com.shz.pojo.*;
import com.shz.service.QuestionsService;
import com.shz.service.RecordsService;
import com.shz.service.XuanzeService;
import com.shz.utils.DateUtils;
import com.shz.utils.ListComparator;
import com.shz.utils.UniqueRandomIdGenerator;
import com.shz.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shz
 * @since 2024-09-15
 */
@Api(tags = "答题模块")

@RestController
@RequestMapping("/records")
public class RecordsController {
    @Resource
    RecordsService recordsService;
    @Resource
    QuestionsService questionsService;
    @ApiOperation(value = "创建做题记录")
    @PostMapping("/saveRecordAndGetQuestionsList")
    public Records saveRecordAndGetQuestionsList(){
        Records records=new Records();
        List<Questions> list = questionsService.query().list();
        Set<Integer> integers = UniqueRandomIdGenerator.generateUniqueRandomIds(0, list.size() - 1, 10);
        List<Questions> res=new ArrayList<>();
        List<Integer> numsList=new ArrayList<>();
        for(int x:integers){
            Questions questions = list.get(x);
            questions.setRights("");
            res.add(questions);
            numsList.add(questions.getId());
        }
        String join = StrUtil.join(",", numsList);
        records.setUid(UserUtil.getCurrentUser().getId());
        records.setStartTime(DateUtils.getNowDate());
        records.setNums(join);
        records.setNumsRes(numsList);
        records.setQuestionsList(res);
        recordsService.save(records);
        return records;
    }
    @Resource
    XuanzeService xuanzeService;

    @ApiOperation(value = "完成做题,错题数为10-错题列表的长度")
    @PutMapping("/finishRecord")
    public Records finishRecord(@RequestBody Records records){
        String[] split = records.getNums().split(",");
        List<String> list = Arrays.asList(split);
        int i=0;
        List<MyAnswer> res=new ArrayList<>();
        for (List<String> myAnswer : records.getMyAnswers()) {
            Questions q = questionsService.query().eq("id", Integer.valueOf(list.get(i))).one();
            String[] split1 = q.getRights().split(",");
            List<String> list1 = Arrays.asList(split1);
            if(q.getType()==0){
                if (!Objects.equals(list1.get(0), myAnswer.get(0))) {
                    MyAnswer my = new MyAnswer();
                    my.setId(Integer.valueOf(list.get(i)));
                    my.setMyAnswer(myAnswer);
                    res.add(my);
                }
            }else if(q.getType()==1){
                boolean isTrue = ListComparator.compareListsUsingSort(myAnswer, list1);
                if(!isTrue){
                    MyAnswer my = new MyAnswer();
                    my.setId(Integer.valueOf(list.get(i)));
                    my.setMyAnswer(myAnswer);
                    res.add(my);
                }
            }else{
                List<Xuanze> xuanzeList = xuanzeService.query().eq("qid", Integer.valueOf(list.get(i))).orderByAsc("id").list();
                List<String> collect = xuanzeList.stream().map(Xuanze::getXuanxiang).collect(Collectors.toList());
                boolean isTrue = ListComparator.compareList(myAnswer, collect);
                if(!isTrue){
                    MyAnswer my = new MyAnswer();
                    my.setId(Integer.valueOf(list.get(i)));
                    my.setMyAnswer(myAnswer);
                    res.add(my);
                }
            }
            i++;
        }
        MyAnswerList myAnswerList=new MyAnswerList();
        myAnswerList.setMyAnswerList(res);
        records.setErrorList(JSONUtil.toJsonStr(myAnswerList));
        records.setErrorListRes(res);
        records.setEndTime(DateUtils.getNowDate());
        recordsService.updateById(records);

        return records;
    }
    @ApiOperation(value = "我的做题记录")
    @GetMapping("/getMyRecords")
    public List<Records> getMyRecords(){
        recordsService.remove(new QueryWrapper<Records>().isNull("end_time"));
        List<Records> list = recordsService.query().eq("uid", UserUtil.getCurrentUser().getId()).list();
        for(Records r:list){
            String errorList = r.getErrorList();
            MyAnswerList my = JSONUtil.toBean(errorList, MyAnswerList.class);
            r.setErrorListRes(my.getMyAnswerList());
            String[] join = r.getNums().split(",");
            List<Integer> collect = Arrays.asList(join).stream().map(Integer::valueOf).collect(Collectors.toList());
            r.setNumsRes(collect);
        }
        System.out.println(list.size());
        return list;
    }

}

